//
//  ShellOrderDetailView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/2.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellOrderDetailView.h"
#import "ShellOrderViewModel.h"




@interface  ShellOrderDetailView()

@property (nonatomic,strong) ShellOrderViewModel *viewModel;

@property (nonatomic,strong) UIView *contentView;

@property (nonatomic,strong) UIButton *tradeBtn;


@end

@implementation ShellOrderDetailView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellOrderViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    [self addSubview:self.contentView];
    self.contentView.cornerRadius = 8;
    self.contentView.backgroundColor = UIColorHex(0x08233F);
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(15);
        make.width.mas_equalTo(K_SCREENWIDTH - 30);
        make.height.mas_equalTo(330);
    }];
 
    ///数据
    NSString *type = [self.viewModel.orderModel.type isEqualToString:@"in"] ? LZLocalizedString(@"转入"):LZLocalizedString(@"转出");
    NSString *orderAmount = [NSString stringWithFormat:@"%f",self.viewModel.orderModel.orderAmount].saveDecimal;
    NSString *exchangeRate = [NSString stringWithFormat:@"%f",self.viewModel.orderModel.exchangeRate].saveDecimal;
    NSString *awardUsdt = [NSString stringWithFormat:@"%f",self.viewModel.orderModel.awardUsdt].saveDecimal;
    NSString *targetAddress = [NSString stringWithFormat:@"%@",self.viewModel.orderModel.targetAddress];
    NSString *standardAmount = [NSString stringWithFormat:@"%f",self.viewModel.orderModel.standardAmount].saveDecimal;

    NSArray *details = @[LZLocalizedString(@"订单编号"),self.viewModel.orderModel.code?:@"",
                        LZLocalizedString(@"订单类型"),type,
                        LZLocalizedString(@"订单金额"),orderAmount?:@"",
                        LZLocalizedString(@"转出地址"),targetAddress?:@"",
                        LZLocalizedString(@"本位币种"),self.viewModel.orderModel.standardCurrency?:@"",
                        LZLocalizedString(@"当前汇率"),exchangeRate,
                        LZLocalizedString(@"折算金额"),standardAmount,
                        LZLocalizedString(@"服务奖励"),[NSString stringWithFormat:@"%@ USDT",awardUsdt],
                        LZLocalizedString(@"订单时间"),self.viewModel.orderModel.createTime?:@"",
                        LZLocalizedString(@"交易时间"),self.viewModel.orderModel.dealTime?:@""];

    
    NSInteger status = [self.viewModel.orderModel.status integerValue];
    if(status != 10){
        self.tradeBtn.hidden = YES;
    }
    
    [self.tradeBtn setTitle:[self.viewModel.orderModel.type isEqualToString:@"in"] ? LZLocalizedString(@"接收") :LZLocalizedString(@"转出")  forState:UIControlStateNormal];
    
    
    //每个Item宽高
    CGFloat H = 20;
    CGFloat W = (K_SCREENWIDTH - 30 - 30 - 10)/2.0;
    //每行列数
    NSInteger rank = 2;
    //每列间距
    CGFloat rankMargin = 10;
    //每行间距
    CGFloat rowMargin = 9;
    //Item索引 ->根据需求改变索引
    NSUInteger index = details.count;
    //Item top
    CGFloat top = 23;
    
    for (int i = 0 ; i< index; i++) {
        UILabel *detailLab = [UILabel lz_labelWithText:details[i] fontSize:k_textFont(14) color:UIColorHex(0xFFFFFF)];
        detailLab.adjustsFontSizeToFitWidth = YES;
        //Item X轴
        CGFloat X = (i % rank) * (W + rankMargin) + 20;
        //Item Y轴
        NSUInteger Y = (i / rank) * (H +rowMargin);
        if(i%2 == 0){
            detailLab.textColor = UIColorHex(0x8D99B2);
            
            if(i == 0){
                W = 100;
            } else {
                W = 150;
            }
            
        } else {
            [detailLab lz_rightAlignment];
            detailLab.textColor = UIColorHex(0xFFFFFF);
            if(i == 1){
                W = K_SCREENWIDTH - 100 - 30 - 10 - 30;
            } else {
                W = K_SCREENWIDTH - 150 - 30 - 10 - 30;
            }
                       
        }
    
        detailLab.frame = CGRectMake(X, Y + top, W, H);
        [self.contentView addSubview:detailLab];
    }
    
    
    [self addSubview:self.tradeBtn];
    [self.tradeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(K_SCREENWIDTH - 30);
        make.height.mas_equalTo(50);
    }];
    
    UIImage *highlightImage = [UIImage gradientImageWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] rect:CGRectMake(0, 0, K_SCREENWIDTH - 30, 50)];
    [self.tradeBtn setBackgroundImage:highlightImage forState:UIControlStateNormal];
    self.tradeBtn.cornerRadius = 6;


    __block NSInteger timeCount = self.viewModel.orderModel.remainingSeconds - 1;
    @weakify(self);
    //button type要 设置成custom 否则会闪动
    self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * _Nullable x) {
        @strongify(self)
        //时间 --
        NSLog(@"发送验证码 %ld",(long)timeCount);
        
        timeCount -- ;
        if(timeCount < 0){
            timeCount = 0;
            [self.disposable dispose];
            self.disposable = nil;
        }
        //设置文字
        NSString *time = [NSString stringWithFormat:@"%02zd:%02zd", (timeCount/60)%60, timeCount%60];

        
        NSString *statues = [self.viewModel.orderModel.type isEqualToString:@"in"] ? LZLocalizedString(@"接收") :LZLocalizedString(@"转出");
        NSString *btnText = [NSString stringWithFormat:@"%@ (%@)",statues,time];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongify(self)
            [self.tradeBtn setTitle:btnText forState:UIControlStateNormal];
        });
    }];

}

//转出
- (void)tradeBtnClick:(UIButton *)sender{
    
    @weakify(self);
    [LZToolView showAlertType:LZAlertViewTypeTradePassword withTitle:LZLocalizedString(@"交易密码") message:LZLocalizedString(@"请输入交易密码") cancel:LZLocalizedString(@"取消") sure:LZLocalizedString(@"确定") objectDict:@{} cancelBlock:^(id  _Nullable object) { } sureBlock:^(id  _Nullable object) {
        @strongify(self);
        //发送请求
        [self.viewModel tradeProcess:self.viewModel.orderModel.code payPassword:object];
    }];

    
    
}


- (UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc] init];
    }
    return _contentView;
}


- (UIButton *)tradeBtn{
    if(!_tradeBtn){
        _tradeBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"确定") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
        [_tradeBtn addTarget:self action:@selector(tradeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _tradeBtn;
}
@end
